;$(function () {
    var serverIdArr=[];
    var id = $("#id").val();
    contribute_validator();
    if(id==null||id==""){
    	upload(serverIdArr);
    }else{
    	getInfo(id, serverIdArr);
    }
});

var tale = new $.tale();//弹框
function upload(serverIdArr){
    var myDropzone = new Dropzone("#dropzone", {
        paramName: "file",
        url: ctxPath+"/file/upload",
        addRemoveLinks: true,
        dictRemoveLinks: "x",
//        	        uploadMultiple: true,
        maxFiles: 3,
        maxFilesize: 10,
        init: function () {
            this.on("success", function (file, msg) {
                file.serverId=msg.message;
                serverIdArr.push(msg.message);
                $("#image_files").val(serverIdArr);
            });
            this.on('error', function (files, response) {
            });
            //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
            this.on("removedfile", function (file) {
                serverIdArr.splice($.inArray(file.serverId,serverIdArr),1);
                $("#image_files").val(serverIdArr);
            });
        }
    });
}

function contribute_validator() {
    $("#global-form").validator({
        fields: {
            type: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            title: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
            files: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
        },
        invalid: function (form, errors) {
            return;
        },
        //验证成功
        valid: function (form) {
            saveInfo();
        },
    });
}


function getInfo(id,serverIdArr){
    $.ajax({
        url: ctxPath+"/party/class/findById/"+id,
        type: 'GET',
        success: function(d){
            var success = d.success;
            if(success==true){
                var data=  d.data;
                umbinding.bind(data);
                ue.ready(function(){//监听编辑器实例化完成的事件
                    if(data.basicFunction!=null){
                        ue.setContent(data.basicFunction);
                    }
                });
                serverIdArr.push(data.files);
                $("#image_files").val(serverIdArr);
                var myDropzone = new Dropzone("#dropzone", {
                    paramName: "file",
                    url: ctxPath+"/file/upload",
                    addRemoveLinks: true,
                    dictRemoveLinks: "x",
//        	        uploadMultiple: true,
                    maxFiles: 3,
                    maxFilesize: 10,
                    init: function () {
                        this.on("success", function (file, msg) {
                            file.serverId=msg.message;
                            serverIdArr.push(msg.message);
                            $("#image_files").val(serverIdArr);
                        });
                        this.on('error', function (files, response) {
                        });
                        //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
                        this.on("removedfile", function (file) {
                            serverIdArr.splice($.inArray(file.serverId,serverIdArr),1);
                            $("#image_files").val(serverIdArr);
                        });
                    }
                });
                if(data.files!=null){
                    for (var i = 0; i < data.fileEOList.length; i++) {
                        var size = data.fileEOList[i].size*1024;
                        var mockFile = {
                            name: data.fileEOList[i].srcname,
                            accepted:true,
                            size: size, //图片尺寸
                            height:"70%",
                            width:"70%",
                            type:'.jpg,.png,.gif'//图片文件类型
                        };
                        myDropzone.emit("addedfile", mockFile);
                        myDropzone.emit("thumbnail", mockFile, ctxPath+"/file/showImage/"+data.fileEOList[i].id);
                        myDropzone.emit("complete", mockFile);
                    }
                }
            }
        },
        error:function(){
            var myDropzone = new Dropzone("#dropzone", {
                paramName: "file",
                url: "/file/upload",
                addRemoveLinks: true,
                dictRemoveLinks: "x",
//        	        uploadMultiple: true,
                maxFiles: 3,
                maxFilesize: 10,
                init: function () {
                    this.on("success", function (file, msg) {
                        file.serverId=msg.message;
                        serverIdArr.push(msg.message);
                        $("#image_files").val(serverIdArr);
                    });
                    this.on('error', function (files, response) {
                    });
                    //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
                    this.on("removedfile", function (file) {
                        serverIdArr.splice($.inArray(file.serverId,serverIdArr),1);
                        $("#image_files").val(serverIdArr);
                    });
                }
            });
        }
    });
}

function saveInfo() {
    var type = $("#type").val();
    if (type == '-1') {
        alert("请选择分类");
        return;
    }
    var json =$("#global-form").serializeFormJSON();
    $.ajax({
        url: ctxPath+"/party/class/saveClassInfo",
        type: 'POST',
        data: json,
        success: function (d) {
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '课程保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = ctxPath+'/party/class/list';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
        }
    });
}
